package Base.多线程通信;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈鹏
 * 问题就是供求关系不匹配，生产和消费对不上，这时候就需要用线程通信
 */
public class Problem {
    public static void main(String[] args) {
        List<Object> goods = new ArrayList<>();
        long start = System.currentTimeMillis();
        Thread thread1 = new Thread(() -> {
            int num = 0;
            while (System.currentTimeMillis()-start<=100){
                goods.add("商品" + ++num);
                System.out.println("生产商品"+num);
            }
        },"生产者");
        Thread thread2 = new Thread(() -> {
            int num = 0;
            while (System.currentTimeMillis()-start<=100){
                goods.remove("商品"+ ++num);
                System.out.println("消费商品"+num);
            }
        },"生产者");
//        定义了两个线程，分别是消费者和生产者，同时运行100毫秒，看看供求关系
        thread1.start();
        thread2.start();
    }
}
